package com.lry.protocol;

import com.lry.common.ProtocolConstants;
import com.lry.compressor.CompressorType;
import com.lry.serialize.SerializerType;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
public class RpcMessage implements Serializable {

    private static final long serialVersionUID = 5359096060555795690L;

    private int requestId;//请求id，幂等处理

    private byte messageType;//消息类型，request，response，heartbeat_req,heartbeat_resp

    private byte serializer = ProtocolConstants.CONFIGURED_SERIALIZER;//序列化code

    private byte compressor = ProtocolConstants.CONFIGURED_COMPRESSOR;//压缩code

    private Map<String,String> headMap = new HashMap<>();

    private Object body;

    public RpcMessage(){

    }

    public String getHead(String key){
        return headMap.get(key);
    }

    public void addHeads(Map<String,String> heads){
        headMap.putAll(heads);
    }

    public void addHead(String key,String value){
        headMap.put(key,value);
    }

    public void setSerializerStr(String serializer) {
        SerializerType serializerType = SerializerType.getByName(serializer);
        if(null!=serializerType){
            this.serializer = serializerType.getCode();
        }
    }

    public void setCompressorStr(String compressor) {
        CompressorType compressorType = CompressorType.getByName(compressor);
        if(null!=compressorType){
            this.compressor = compressorType.getCode();
        }
    }

    public RpcMessage(int requestId, byte messageType, Object body){
        this.requestId = requestId;
        this.messageType = messageType;
        this.body = body;
    }

    public RpcMessage(int requestId){
        this.requestId = requestId;
    }


}
